//http://topic.csdn.net/u/20110325/09/2f26512b-8c02-4973-9c38-b97410bd9679.html

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
void RetrieveValidPointIds(string filename,map<int,int>& mapPointIds)
{
	ifstream fin(filename.c_str());
	string line;
	while(getline(fin,line,'\n'))
	{
		int idx1 = line.find_first_of(':');
		int id = atoi(line.substr(0,idx1).c_str());
		int equalIdx = line.find('=');
		int commaIdx = line.find(',',equalIdx);
		double x = atof(line.substr(equalIdx + 1,commaIdx - equalIdx - 1).c_str());
		int equalIdx2 = line.find('=',commaIdx);
		int commaIdx2 = line.find(',',equalIdx2);
		double y = atof(line.substr(equalIdx2 + 1,commaIdx2 - equalIdx2 - 1).c_str());
		int equalIdx3 = line.find('=',commaIdx2);
		int commaIdx3 = line.find(',',equalIdx3);
		double z = atof(line.substr(equalIdx3 + 1,commaIdx3 - equalIdx3 - 1).c_str());

		if(x > 25 && z > 25)
		//if (x > 0 && z > 20)
		{
			mapPointIds[id] = 1;
			//cout << id << endl;
		}

		//cout << line << endl;
	}

}
void RetrieveUnitIds(string filename,map<int,int>& mapPointIds,map<int,int>& mapUnitIds)
{
	ifstream fin(filename.c_str());
	int unitId;
	int pId[4];
	while (fin >> unitId)
	{
		for (int i = 0;i<4;i++)
		{
			fin >> pId[i];
		}
		for (int i = 0;i<4;i++)
		{
			if (mapPointIds.find(pId[i]) != mapPointIds.end())
			{
				mapUnitIds[unitId] = 1;
				break;
			}
		}
	}
}
double GetMaxVal(string filename,map<int,int>& mapUnitIds)
{
	ifstream fin(filename.c_str());
	int unitId;
	double val;
	double maxVal = -10000000.0;
	while(fin >> unitId >> val)
	{
		if (mapUnitIds.find(unitId)!=mapUnitIds.end())
		{
			if (val > maxVal)
			{
				maxVal = val;
			}
		}
	}
	return maxVal;
}

int main(int argc,char* argv[])
{
	string filename1 = "1.txt";
	string filename2 = "2.txt";
	string filename3 = "3.txt";

	map<int,int> mapPointsIds;
	RetrieveValidPointIds(filename1,mapPointsIds);
	map<int,int> mapUnitIds;
	RetrieveUnitIds(filename3,mapPointsIds,mapUnitIds);

	double maxVal = GetMaxVal(filename2,mapUnitIds);
	cout << maxVal << endl;
}